﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ProgramWindow
{
    //handycontrol
    //https://handyorg.github.io/handycontrol/

    //sqlsugar
    //https://www.donet5.com/Home/Doc

    //icons
    //https://materialdesignicons.com/

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //鼠标移动颜色
        SolidColorBrush leaveColor = new SolidColorBrush(Color.FromRgb(58, 144, 193));
        SolidColorBrush moveColor = new SolidColorBrush(Color.FromRgb(44, 177, 255));
        SolidColorBrush selectColor = new SolidColorBrush(Color.FromRgb(100, 185, 255));

        public static ViewModel.MainViewModel viewModel = new ViewModel.MainViewModel();
        MesTool.Model.UserModel userModel = new MesTool.Model.UserModel();
        //软件名
        string title = "惠科烧录工具";
        //软件版本
        string ver = "Ver.22.08.02.0001";

        //子界面
        View.DemoView demoView = null;
        View.ProgramView programView = null;

        public MainWindow()
        {
            try
            {
                //登录界面
                //历史账号
                List<string> ids = new List<string> {  };
                bool? reslut = new BaseView.View.LogInView(userModel, ids).ShowDialog();
                if (reslut == false)
                {
                    this.Close();
                    return;
                }
                //设置中文
                HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-CN");
                InitializeComponent();

                //登录人员
                viewModel.User = userModel.nickName;
                //标题
                viewModel.Title = title;

                //时间显示
                DispatcherTimer dispatcherTimer = new DispatcherTimer(new TimeSpan(TimeSpan.TicksPerMillisecond * 100), DispatcherPriority.Normal, delegate
                {
                    viewModel.Time = DateTime.Now.ToString("G");
                }, Dispatcher);
                DataContext = viewModel;
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message, "错误");
                BaseTool.LogHelper.WriteError(ex.Message, ex);
            }
        }
        /// <summary>
        /// 菜单栏颜色切换
        /// </summary>
        /// <param name="selectBorder">选中菜单</param>
        private void ChangeMenuColor(Border selectBorder)
        {
            foreach (var item in StackPanelMenu.Children)
            {
                if (item is Border)
                {
                    Border border = item as Border;
                    if (border == selectBorder)
                    {
                        border.Background = selectColor;

                    }
                    if (border != selectBorder)
                    {
                        border.Background = leaveColor;
                    }
                }
            }
        }
        #region 窗口
        /// <summary>
        /// 窗口移动事件
        /// </summary>
        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
        int i = 0;
        /// <summary>
        /// 标题栏双击事件
        /// </summary>       
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            i += 1;
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            timer.Tick += (s, e1) => { timer.IsEnabled = false; i = 0; };
            timer.IsEnabled = true;
            if (i % 2 == 0)
            {
                timer.IsEnabled = false;
                i = 0;
                //this.WindowState = this.WindowState == WindowState.Maximized ?
                //              WindowState.Normal : WindowState.Maximized;
            }
        }
        private void PackIcon_MouseLeave(object sender, MouseEventArgs e)
        {
            PackIcon packIcon = sender as PackIcon;
            packIcon.Background = leaveColor;
        }

        private void PackIcon_MouseMove(object sender, MouseEventArgs e)
        {
            PackIcon packIcon = sender as PackIcon;
            packIcon.Background = moveColor;
        }
        private void PackIconClose_MouseMove(object sender, MouseEventArgs e)
        {
            PackIcon packIcon = sender as PackIcon;
            packIcon.Background = Brushes.Tomato;
        }
        private void PackIconMenu_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            contextMenu.PlacementTarget = sender as PackIcon;
            contextMenu.Placement = PlacementMode.Bottom;
            contextMenu.HorizontalOffset = -126;
            //显示菜单
            contextMenu.IsOpen = true;
        }
        private void PackIconMin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void PackIconClose_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            //写日志
            BLL.AppBLL.CloseApp();
            BaseTool.LogHelper.WriteDebug("软件退出");
            Environment.Exit(0);
        }
        #endregion

        #region 菜单栏
        private void Menu_Leave(object sender, MouseEventArgs e)
        {
            Border border = sender as Border;
            if ((border.Background as SolidColorBrush).Color.R != selectColor.Color.R)
            {
                border.Background = leaveColor;
            }
        }

        private void Menu_Move(object sender, MouseEventArgs e)
        {
            Border border = sender as Border;
            if ((border.Background as SolidColorBrush).Color.R != selectColor.Color.R)
            {
                border.Background = moveColor;
            }
        }

        #endregion



        private void MenuAbout_Click(object sender, RoutedEventArgs e)
        {
            //关于界面
            new BaseView.View.AboutView(title, ver).ShowDialog();
        }

        private void MenuLog_Click(object sender, RoutedEventArgs e)
        {
            //打开日志目录
            System.Diagnostics.Process.Start("Explorer.exe", Environment.CurrentDirectory + "\\Log");
        }

        private void MenuLogIn_Click(object sender, RoutedEventArgs e)
        {
            //历史账号
            List<string> ids = null;
            bool? reslut = new BaseView.View.LogInView(userModel, null).ShowDialog();
            if (reslut == false)
            {
                return;
            }
            viewModel.User = userModel.nickName;
        }

        private void MenuTest_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ////判断已经选中
            if (GridMain.Children.Contains(programView) == false)
            {
                //改变颜色
                Border selectBorder = sender as Border;
                ChangeMenuColor(selectBorder);
                //弹出子页面
                GridMain.Children.Clear();
                GridMain.Children.Add(programView);
            }
        }

        private void MenuEdit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void MenuData_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void MenuSet_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //实例化子界面
            demoView = new View.DemoView();
            if (programView == null)
            {
                bool isOpen = false;
                programView = new View.ProgramView(this, ref isOpen);
                if (isOpen == false)
                {
                    this.Close();
                    return;
                }
            }
            //弹出子页面
            GridMain.Children.Clear();
            GridMain.Children.Add(programView);
        }

        private void Window_Closed_1(object sender, EventArgs e)
        {

        }
    }
}
